package com.example.p2pinvest.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.p2pinvest.R;
import com.example.p2pinvest.common.BaseFragment;
import com.example.p2pinvest.util.UIUtils;
import com.loopj.android.http.RequestParams;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/2
 * 投资fragment
 */
public class InvestFragment extends BaseFragment {


    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_title_setting)
    ImageView ivTitleSetting;
    @BindView(R.id.tabpage_invest)
    TabPageIndicator tabPageInvest;
    @BindView(R.id.vp_invest)
    ViewPager vpInvest;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return null;
    }

    @Override
    public void initData(String content) {
        //加载三个不同的fragment : ProductListFragment  ProductRecommendFragment  ProductHotFragment
        initFragments();
        //ViewPager设置三个fragment的显示
        vpInvest.setAdapter(new MyAdapter(getParentFragmentManager()));
        //将tabPageInvest与viewPager关联
        tabPageInvest.setViewPager(vpInvest);
    }

    private List<Fragment> fragmentList = new ArrayList<>();

    private void initFragments() {
        ProductListFragment productListFragment = new ProductListFragment();
        ProductRecommendFragment productRecommendFragment = new ProductRecommendFragment();
        ProductHotFragment productHotFragment = new ProductHotFragment();

        //添加到集合中
        fragmentList.add(productListFragment);
        fragmentList.add(productRecommendFragment);
        fragmentList.add(productHotFragment);
    }

    @Override
    public void initTitle() {
        ivTitleBack.setVisibility(View.GONE);
        tvTitle.setText("投资");
        ivTitleSetting.setVisibility(View.GONE);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_invest;
    }

    /**
     * 提供pagerAdapter的实现
     * 如果viewPager中加载的是fragment,则提供的Adapter可以继承于具体的 FragmentStatePagerAdapter 和 FragmentPagerAdapter
     */
    class MyAdapter extends FragmentPagerAdapter {


        public MyAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList == null ? 0 : fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {

            ///根据已有资源获取每个fragment的标题
            return UIUtils.getStringArr(R.array.invest_tab)[position];
        }
    }
}
